package com.cltech.controller.sys;

import com.cltech.dto.AuthorizationDto;
import com.cltech.dto.SysRoleDto;
import com.cltech.entity.sys.SysRole;
import com.cltech.frame.result.PageResult;
import com.cltech.frame.result.ResponseBean;
import com.cltech.mapper.SysRoleMapper;
import com.cltech.service.sys.SysPermissionService;
import com.cltech.service.sys.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by Administrator on 2019/3/6.
 */
@RestController
@RequestMapping("api/role")
@Api(tags = {"角色管理"})
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    protected SysPermissionService sysPermissionService;
    @PostMapping
    @ApiOperation("新增或编辑角色")
    public Object addOrUpdate(@RequestBody @Valid SysRoleDto sysRoleDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseBean.fail(bindingResult.getFieldError().getDefaultMessage());
        } else {
            return sysRoleService.addOrUpdate(sysRoleDto);
        }
    }

    @GetMapping
    @ApiOperation("角色列表查询")
    public Object list(HttpServletRequest request, @RequestParam(value="page",defaultValue="0")int page,
                       @RequestParam(value="size",defaultValue="10")int size) {
        return ResponseBean.success(PageResult.result(sysRoleService.findAll(request.getParameterMap()
                ,page, size)));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取角色")
    public Object get(@PathVariable String id) {
        return sysRoleService.findById(id);
    }



    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable String id) {
        return sysRoleService.delete(id);
    }

    @PostMapping("/authorization")
    @ApiOperation("授权管理")
    public Object authorization(@RequestBody @Valid AuthorizationDto authorizationDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseBean.fail(bindingResult.getFieldError().getDefaultMessage());
        } else {
            return sysRoleService.authorization(authorizationDto);
        }
    }

    @GetMapping("/findPermissionsTreeByRole")
    @ApiOperation("查找角色下所有权限资源树")
    public Object findPermissionsTreeByRole(String roleId, String sysPermissionType, Integer sysPermissionlevel) {
        return sysPermissionService.findPermissionsTreeByRole(roleId, sysPermissionType, sysPermissionlevel);
    }
    @GetMapping("/findAllPermissionsTreeByRole")
    @ApiOperation("查找角色下所有权限资源树(授权选中，未授权不选中)")
    public Object findAllPermissionsTreeByRole(String roleId, String sysPermissionType, Integer sysPermissionlevel) {
        return sysPermissionService.findAllPermissionsTreeByRole(roleId, sysPermissionType, sysPermissionlevel);
    }
}
